package com.ruoyi.bearing.controller;


import com.ruoyi.bearing.domain.BearingBasicInfo;
import com.ruoyi.bearing.service.IBearingBasicInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.flask.FlaskException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门Controller
 *
 * @date 2023-06-30
 */
@RestController
@RequestMapping("/bearing/basic")
public class BearingBasicInfoController extends BaseController {
    @Autowired
    private IBearingBasicInfoService bearingBasicInfoService;

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:list')")
    @GetMapping("/list")
    public TableDataInfo list(BearingBasicInfo bearingBasicInfo) {
        startPage();
        List<BearingBasicInfo> list = bearingBasicInfoService.selectBearingBasicInfoList(bearingBasicInfo);
        return getDataTable(list);
    }

    /**
     * 查询部门列表
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:test')")
    @GetMapping("/test")
    public AjaxResult testModelTrainSystem(BearingBasicInfo bearingBasicInfo) {
        String res = bearingBasicInfoService.startTestModelTrainSystem();
        if ("".equals(res)) {
            throw new FlaskException();
        }
        return success(res);
    }

    /**
     * 导出部门列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:export')")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BearingBasicInfo bearingBasicInfo) {
        List<BearingBasicInfo> list = bearingBasicInfoService.selectBearingBasicInfoList(bearingBasicInfo);
        ExcelUtil<BearingBasicInfo> util = new ExcelUtil<BearingBasicInfo>(BearingBasicInfo.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bearingBasicInfoService.selectBearingBasicInfoById(id));
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:add')")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BearingBasicInfo bearingBasicInfo) {
        return toAjax(bearingBasicInfoService.insertBearingBasicInfo(bearingBasicInfo));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:edit')")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BearingBasicInfo bearingBasicInfo) {
        return toAjax(bearingBasicInfoService.updateBearingBasicInfo(bearingBasicInfo));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('bearing:basic:remove')")
    @Log(title = "部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bearingBasicInfoService.deleteBearingBasicInfoByIds(ids));
    }
}
